﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using localText = Proxy.Properties.Resources;

namespace HomagChina.CCT.ViewModels
{
    public class HomePlateDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_Modify;
        private DeleteInfoHandle Handle_Delete;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性
        public RelayCommand ApplyCommand { get; }
        public RelayCommand ModifyCommand { get; }
        public RelayCommand DeleteCommand { get; }
        public RelayCommand LoadedCommand { get; }

        BoardInfoModel model = new BoardInfoModel();

        private string _OrderId;
        public string OrderId
        {
            get => _OrderId;
            set { _OrderId = value; OnPropertyChanged(); }
        }

        private float _Length;
        public float Length
        {
            get => _Length;
            set { _Length = value; OnPropertyChanged(); }
        }

        private float _Width;
        public float Width
        {
            get => _Width;
            set { _Width = value; OnPropertyChanged(); }
        }

        private float _Thickness;
        public float Thickness
        {
            get => _Thickness;
            set { _Thickness = value; OnPropertyChanged(); }
        }

        private string _Reference;
        public string Reference
        {
            get => _Reference;
            set { _Reference = value; OnPropertyChanged(); }
        }

        private int _OrderPackageCount;
        public int OrderPackageCount
        {
            get => _OrderPackageCount;
            set { _OrderPackageCount = value; OnPropertyChanged(); }
        }

        private string _EditVisible;
        public string EditVisible
        {
            get => _EditVisible;
            set { _EditVisible = value; OnPropertyChanged(); }
        }

        private bool _IsEnginOrder = false;
        public bool IsEnginOrder
        {
            get => _IsEnginOrder;
            set { _IsEnginOrder = value; OnPropertyChanged(); }
        }

        private string _SaveVisible;
        public string SaveVisible
        {
            get => _SaveVisible;
            set { _SaveVisible = value; OnPropertyChanged(); }
        }

        private string _TemplateVisible;
        public string TemplateVisible
        {
            get => _TemplateVisible;
            set { _TemplateVisible = value; OnPropertyChanged(); }
        }

        private bool _IsReadOnly;
        public bool IsReadOnly
        {
            get => _IsReadOnly;
            set { _IsReadOnly = value; OnPropertyChanged(); }
        }

        private bool _CheckState;
        public bool CheckState
        {
            get => _CheckState;
            set { _CheckState = value; OnPropertyChanged(); }
        }

        private object _SelectedValue = "10";

        public object SelectedValue
        {
            get => _SelectedValue;
            set { _SelectedValue = value; OnPropertyChanged(); }
        }

        private object _SelectedValue_Template;

        public object SelectedValue_Template
        {
            get => _SelectedValue_Template;
            set
            {
                _SelectedValue_Template = value;
                FillupInfo();
                OnPropertyChanged();
            }
        }

        private int _SelectedIndex_Template;

        public int SelectedIndex_Template
        {
            get => _SelectedIndex_Template;
            set
            {
                _SelectedIndex_Template = value;
                OnPropertyChanged();
            }
        }


        private object _SelectedValue_Machine;
        public object SelectedValue_Machine
        {
            get => _SelectedValue_Machine;
            set
            {
                _SelectedValue_Machine = value;
                OnPropertyChanged();
            }
        }

        private object _SelectedValue_ZeroLine;
        public object SelectedValue_ZeroLine
        {
            get => _SelectedValue_ZeroLine;
            set
            {
                _SelectedValue_ZeroLine = value;
                OnPropertyChanged();
            }
        }
        #endregion 属性

        #region components
        public DropDownList DropDownList_Location { get; set; }
        public DropDownList DropDownList_Template { get; set; }
        public DropDownList DropDownList_Machine { get; set; }
        public DropDownList DropDownList_ZeroLine { get; set; }
        public InputField _InputField { get; set; }
        public InputField InputField
        {
            get => _InputField;
            set { _InputField = value; }
        }
        #endregion components
        public HomePlateDialogViewModel(BoardInfoModel ModifyboardInfoModel, bool isEdit, bool isEnginOrder = false)
        {
            model = ModifyboardInfoModel;
            IsEnginOrder = !isEnginOrder;
            this.EditVisible = isEdit ? "Visible" : "Collapsed";
            this.SaveVisible = isEdit ? "Collapsed" : "Visible";
            this.TemplateVisible = isEdit ? "Hidden" : "Visible";
            this.IsReadOnly = isEdit;
            ApplyCommand = new RelayCommand(Apply);
            ModifyCommand = new RelayCommand(Modify);
            DeleteCommand = new RelayCommand(Delete);
            LoadedCommand = new RelayCommand(ViewLoadedCommand);
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Delete = new DeleteInfoHandle();
            commonHandle = new CommonHandle();
            LoadDropDownList();
        }

        public void ViewLoadedCommand(object InputFieldInfo)
        {
            if (!IsEnginOrder)
            {
                var reference = Handle_Search.GetProjectOrderDetail() == null
                                ? "" : Handle_Search.GetProjectOrderDetail().Reference;
                if (string.IsNullOrWhiteSpace(reference))
                {
                    return;
                }
                var DetailInfo = Handle_Search.GetPartsInfo(reference).FirstOrDefault();
                if (DetailInfo != null)
                {
                    this.Length = (float)DetailInfo.Length;
                    this.Width = (float)DetailInfo.Width;
                    this.Thickness = (float)DetailInfo.Thickness;
                    this.Reference = DetailInfo.Reference;
                    this.CheckState = DetailInfo.PA_Angle == 1;
                    this.SelectedValue_Machine = DetailInfo.Feature == "1" ? "H330" : "Both";
                    this.SelectedValue_Template = DetailInfo.Reference;
                }
            }

            if (model != null)
            {
                this.Reference = model.Reference;
                this.Length = (float)model.Length;
                this.Width = (float)model.Width;
                this.Thickness = (float)model.Thickness;
                this.SelectedValue_Machine = "H330";
                this.SelectedValue_ZeroLine = model.Feature == "L" ? "Left" : model.Feature == "R" ? "Right" : "Both";
            }
        }

        #region DropDownList

        /// <summary>
        /// 加载DropDownList内容
        /// Load the DropDownList
        /// </summary>
        public void LoadDropDownList()
        {
            //DropDownList_Location = new DropDownList
            //{
            //    ItemsSource = (Handle_Search.GetLocationInfo(true))
            //};
            if (!IsEnginOrder)
            {
                DropDownList_Template = new DropDownList
                {
                    ItemsSource = (Handle_Search.GetProjectInfo())
                };
            }
            DropDownList_Machine = new DropDownList
            {
                ItemsSource = new List<string>() { { "H330" } }//, { "Both" }
            };
            DropDownList_ZeroLine = new DropDownList
            {
                ItemsSource = new List<string>() { { "Both" }, { "Left" }, { "Right" } }
            };
        }

        /// <summary>
        ///
        /// </summary>
        public void FillupInfo()
        {
            if (!string.IsNullOrWhiteSpace(SelectedValue_Template.ToString()))
            {
                var DetailInfo = Handle_Search.GetPartsInfo(SelectedValue_Template.ToString()).FirstOrDefault();
                this.Length = (float)DetailInfo.Length;
                this.Width = (float)DetailInfo.Width;
                this.Thickness = (float)DetailInfo.Thickness;
                this.Reference = DetailInfo.Reference;
                this.CheckState = DetailInfo.PA_Angle == 1;
                this.SelectedValue_Machine = "H330";
                this.SelectedValue_ZeroLine = DetailInfo.Feature == "B" ? "Both"
                                                : DetailInfo.Feature == "L" ? "Left" : "Right";
            }
            else
            {
                this.Length = 0;
                this.Width = 0;
                this.Thickness = 0;
                this.Reference = string.Empty;
                this.CheckState = false;
            }
        }

        #endregion DropDownList

        //提交函数
        private void Apply()
        {
            T_BoardInfo t_BoardInfo = new T_BoardInfo()
            {
                Reference = Reference,
                Length = Length,
                Width = Width,
                Thickness = Thickness,
                BatchId = "Project-" + Reference,
                MprName = @"D:\control\m1-c1\data\cnc\MP4\" + Reference + ".mpr",
                Feature = SelectedValue_Machine.ToString() == "Both" ? "2" : "1",
                PA_Angle = CheckState ? 1 : 0,
                ColorEdgeBelt = SelectedValue_ZeroLine.ToString() == "Both" ? "0" :
                          SelectedValue_ZeroLine.ToString() == "Left" ? "1" : "2",
            };

            //写入
            if (Handle_Insert.InsertProjectOrder(Reference, Length, Width, Thickness))
            {
                Handle_Insert.InsertBoradInfo(t_BoardInfo);
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Added_successfully);
                commonHandle.CloseDiaLog(localText.PartDialog);
            }
            else
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Add_Failure);
            }
        }

        private void Modify()
        {
            //编辑
            if (Handle_Modify.ModifyBoardInfo(Width, Length, Thickness, SelectedValue_ZeroLine.ToString(), Reference))
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Update_successful);
                commonHandle.CloseDiaLog(localText.PartDialog);
            }
            else
            {
                HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Update_failed);
            }
        }

        private void Delete()
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(Utility.GetLocalText("Delete Tip") + "\r\n", Utility.GetLocalText("Prompt message"), MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                //编辑
                if (Handle_Delete.DeleteBoardInfoByUpi(model.OnlineId))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Deleted_successfully);
                    commonHandle.CloseDiaLog(localText.PartDialog);
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Delete_Failure);
                }
            }
        }
    }
}